*** ALTERNATIVE ADOPTION DEFINITIONS
 
use "$P_Data_Processed/daily_station_data_e5_7_21.dta", clear

drop avgdate break_date treat_group treat_period month_treat_period

	merge m:1 StID using "$P_Data_Processed/new_adopter_measures_2o3_noPCRes.dta"

gener break_date = avgdate if _merge==3
gener treat_group_2o3_noPCRes = 1 if _merge==3
replace break_date = 10^5 if _merge==1
replace treat_group_2o3_noPCRes = 0 if missing(treat_group_2o3_noPCRes)
drop _merge

gener treat_period_2o3_noPCRes = 1 if weekly_date>=break_date
replace treat_period_2o3_noPCRes = 0 if weekly_date<break_date

gegen month_treat_period_2o3_noPCRes = max(treat_period_2o3_noPCRes), by(StID year_month)

	drop break_date 
	
	merge m:1 StID using "$P_Data_Processed/new_adopter_measures_2o3_noOil.dta"

gener break_date = avgdate if _merge==3
gener treat_group_2o3_noOil = 1 if _merge==3
replace break_date = 10^5 if _merge==1
replace treat_group_2o3_noOil = 0 if missing(treat_group_2o3_noOil)
drop _merge

gener treat_period_2o3_noOil = 1 if weekly_date>=break_date
replace treat_period_2o3_noOil = 0 if weekly_date<break_date

gegen month_treat_period_2o3_noOil = max(treat_period_2o3_noOil), by(StID year_month)

	drop break_date 
	
	merge m:1 StID using "$P_Data_Processed/adopter_measures_2week_e5.dta"

gener break_date = avgdate if _merge==3
gener treat_group_2o4_2w = 1 if _merge==3
replace break_date = 10^5 if _merge==1
replace treat_group_2o4_2w = 0 if missing(treat_group_2o4_2w)
drop _merge

gener treat_period_2o4_2w = 1 if weekly_date>=break_date
replace treat_period_2o4_2w = 0 if weekly_date<break_date

gegen month_treat_period_2o4_2w = max(treat_period_2o4_2w), by(StID year_month)

	drop break_date 
	
	merge m:1 StID using "$P_Data_Processed/adopter_measures_4week_diesel.dta"

gener break_date = avgdate if _merge==3
gener treat_group_diesel = 1 if _merge==3
replace break_date = 10^5 if _merge==1
replace treat_group_diesel = 0 if missing(treat_group_diesel)
drop _merge

gener treat_period_diesel = 1 if weekly_date>=break_date
replace treat_period_diesel = 0 if weekly_date<break_date

gegen month_treat_period_diesel = max(treat_period_diesel), by(StID year_month)

	drop break_date 
	
	merge m:1 StID using "$P_Data_Processed/new_adopter_measures_e5_diesel.dta"

gener break_date = avgdate if _merge==3
gener treat_group_e5_diesel = 1 if _merge==3
replace break_date = 10^5 if _merge==1
replace treat_group_e5_diesel = 0 if missing(treat_group_e5_diesel)
drop _merge

gener treat_period_e5_diesel = 1 if weekly_date>=break_date
replace treat_period_e5_diesel = 0 if weekly_date<break_date

gegen month_treat_period_e5_diesel = max(treat_period_e5_diesel), by(StID year_month)

drop break_date 

merge m:1 StID using "$P_Data_Processed/new_adopter_measures_2o3_pccount_always.dta"

gener break_date = avgdate if _merge==3
gener treat_group_pcc_all = 1 if _merge==3
replace break_date = 10^5 if _merge==1
replace treat_group_pcc_all = 0 if missing(treat_group_pcc_all)
drop _merge

gener treat_period_pcc_all = 1 if weekly_date>=break_date
replace treat_period_pcc_all = 0 if weekly_date<break_date

gegen month_treat_period_pcc_all = max(treat_period_pcc_all), by(StID year_month)


gegen min_date_StID = min(date), by(StID year_month)
keep if date==min_date_StID
drop min_date_StID


	capture drop treat_2o3_noPCRes
	gener treat_2o3_noPCRes = month_treat_period_2o3_noPCRes*treat_group_2o3_noPCRes
	capture drop treat_2o3_noOil
	gener treat_2o3_noOil = month_treat_period_2o3_noOil*treat_group_2o3_noOil
	capture drop treat_2o4_2w
	gener treat_2o4_2w = month_treat_period_2o4_2w*treat_group_2o4_2w
	capture drop treat_diesel
	gener treat_diesel= month_treat_period_diesel*treat_group_diesel
	capture drop treat_e5_diesel
	gener treat_e5_diesel= month_treat_period_e5_diesel*treat_group_e5_diesel
	capture drop treat_pcc_all
	gener treat_pcc_all= month_treat_period_pcc_all*treat_group_pcc_all
		
	
	**
	** drop outlier observations (top and bottom 1%)
	drop if mean_wh_margin<0
	drop if mean_wh_margin>0.2
	drop if mean_price<1.18
	drop if mean_price>1.7
	
	
merge m:1 StID using "$P_Data_Processed/cluster_mkts.dta"
keep if _merge==3
drop _merge

gener station = 1 
gegen n_act_stations = total(station), by(mktid year_month)
by Brand year_month, sort: gener n_brand = _N

** regressions for 2 out of 3, no PCResponse 
	
	capture drop temp_treat
	capture drop n_treat
	capture drop share_others_treated
	gener temp_treat = treat_group_2o3_noPCRes*month_treat_period_2o3_noPCRes
	** generating simple IV - share of adopters in your brand
	gegen n_treat = total(temp_treat), by(Brand year_month)
	gener share_others_treated = (n_treat)/(n_brand-1) if temp_treat==0
	replace share_others_treated = (n_treat-1)/(n_brand-1) if temp_treat==1
	
	capture drop n_mkt_compet_adopt
	gegen n_mkt_compet_adopt = total(temp_treat), by(year_month mktid)
	replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if temp_treat==1
	
	label variable temp_treat "Adopter" 
	
	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_b", replace tex(frag) label  dec(3) keep(temp_treat)

	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_a", replace tex(frag) label  dec(3) keep(temp_treat)	
	
** regressions for 2 out of 3, no oil  
	
	capture drop temp_treat
	capture drop n_treat
	capture drop share_others_treated
	gener temp_treat = treat_group_2o3_noOil* month_treat_period_2o3_noOil
	** generating simple IV - share of adopters in your brand
	gegen n_treat = total(temp_treat), by(Brand year_month)
	gener share_others_treated = (n_treat)/(n_brand-1) if temp_treat==0
	replace share_others_treated = (n_treat-1)/(n_brand-1) if temp_treat==1
	
	capture drop n_mkt_compet_adopt
	gegen n_mkt_compet_adopt = total(temp_treat), by(year_month mktid)
	replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if temp_treat==1
	
	label variable temp_treat "Adopter" 
	
	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_b", append tex(frag) label  dec(3) keep(temp_treat)

	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_a", append tex(frag) label  dec(3) keep(temp_treat)	
	
** regressions for 2 out of 3, PCCount break always
	
	capture drop temp_treat
	capture drop n_treat
	capture drop share_others_treated
	gener temp_treat = month_treat_period_pcc_all*treat_group_pcc_all
	** generating simple IV - share of adopters in your brand
	gegen n_treat = total(temp_treat), by(Brand year_month)
	gener share_others_treated = (n_treat)/(n_brand-1) if temp_treat==0
	replace share_others_treated = (n_treat-1)/(n_brand-1) if temp_treat==1
	
	capture drop n_mkt_compet_adopt
	gegen n_mkt_compet_adopt = total(temp_treat), by(year_month mktid)
	replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if temp_treat==1
	
	label variable temp_treat "Adopter" 
	
	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_b", append tex(frag) label  dec(3) keep(temp_treat)

	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_a", append tex(frag) label  dec(3) keep(temp_treat)	
	
** regressions for 2 out of 4, 2weeks 
	
	capture drop temp_treat
	capture drop n_treat
	capture drop share_others_treated
	gener temp_treat = treat_group_2o4_2w* month_treat_period_2o4_2w
	** generating simple IV - share of adopters in your brand
	gegen n_treat = total(temp_treat), by(Brand year_month)
	gener share_others_treated = (n_treat)/(n_brand-1) if temp_treat==0
	replace share_others_treated = (n_treat-1)/(n_brand-1) if temp_treat==1
	
	capture drop n_mkt_compet_adopt
	gegen n_mkt_compet_adopt = total(temp_treat), by(year_month mktid)
	replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if temp_treat==1
	
	label variable temp_treat "Adopter" 
	
	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_b", append tex(frag) label  dec(3) keep(temp_treat)

	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_a", append tex(frag) label  dec(3) keep(temp_treat)	
	
** regressions for diesel 
	
	capture drop temp_treat
	capture drop n_treat
	capture drop share_others_treated
	gener temp_treat = treat_group_diesel* month_treat_period_diesel
	** generating simple IV - share of adopters in your brand
	gegen n_treat = total(temp_treat), by(Brand year_month)
	gener share_others_treated = (n_treat)/(n_brand-1) if temp_treat==0
	replace share_others_treated = (n_treat-1)/(n_brand-1) if temp_treat==1
	
	capture drop n_mkt_compet_adopt
	gegen n_mkt_compet_adopt = total(temp_treat), by(year_month mktid)
	replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if temp_treat==1
	
	label variable temp_treat "Adopter" 
	
	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_b", append tex(frag) label  dec(3) keep(temp_treat)

	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_a", append tex(frag) label  dec(3) keep(temp_treat)	
	
** regressions for diesel + E5

	
	capture drop temp_treat
	capture drop n_treat
	capture drop share_others_treated
	gener temp_treat = treat_group_e5_diesel* month_treat_period_e5_diesel
	** generating simple IV - share of adopters in your brand
	gegen n_treat = total(temp_treat), by(Brand year_month)
	gener share_others_treated = (n_treat)/(n_brand-1) if temp_treat==0
	replace share_others_treated = (n_treat-1)/(n_brand-1) if temp_treat==1
	
	capture drop n_mkt_compet_adopt
	gegen n_mkt_compet_adopt = total(temp_treat), by(year_month mktid)
	replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if temp_treat==1
	
	label variable temp_treat "Adopter" 
	
	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_b", append tex(frag) label  dec(3) keep(temp_treat)

	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop mean_temp sd_temp mean_precip sd_precip n_brand (temp_treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_a", append tex(frag) label  dec(3) keep(temp_treat)	
	
	
	
**** BIG BREAKS **** 

** create new "selected" treated units: 

clear

use "$P_Data_Processed/weather_pccount_4week_e5.dta", clear
merge m:1 StID using "$P_Data/sBreak/big_breaks_pccount.dta"
keep if _merge==3
drop _merge
save "$P_Data_Processed/weather_pccount_big.dta", replace

clear
use "$P_Data_Processed/pccount_oil_4week_e5.dta"
merge m:1 StID using "$P_Data/sBreak/big_breaks_pccount.dta"
keep if _merge==3
drop _merge
save "$P_Data_Processed/oil_pccount_big.dta", replace

clear
use "$P_Data_Processed/pccount_pcresponse_4week_e5.dta"
merge m:1 StID using "$P_Data/sBreak/big_breaks_pccount.dta"
keep if _merge==3
drop _merge
merge m:1 StID using "$P_Data/sBreak/big_breaks_pcresponse.dta"
keep if _merge==3
drop _merge
save "$P_Data_Processed/pcresponse_pccount_big.dta", replace

clear
use "$P_Data_Processed/weather_pcresponse_4week_e5.dta"
merge m:1 StID using "$P_Data/sBreak/big_breaks_pcresponse.dta"
keep if _merge==3
drop _merge
save "$P_Data_Processed/weather_pcresponse_big.dta", replace

clear
use "$P_Data_Processed/pcresponse_oil_4week_e5.dta"
merge m:1 StID using "$P_Data/sBreak/big_breaks_pcresponse.dta"
keep if _merge==3
drop _merge
save "$P_Data_Processed/oil_pcresponse_big.dta", replace


clear
use "$P_Data_Processed/weather_pcresponse_big.dta"
keep StID avgdate
append using "$P_Data_Processed/weather_pccount_big.dta"
keep StID avgdate
sort StID avgdate
by StID: keep if _n==1
append using "$P_Data_Processed/oil_pccount_big.dta"
keep StID avgdate
sort StID avgdate
by StID: keep if _n==1
append using "$P_Data_Processed/weather_oil.dta"
keep StID avgdate
sort StID avgdate
by StID: keep if _n==1
append using "$P_Data_Processed/pcresponse_pccount_big.dta"
keep StID avgdate
sort StID avgdate
by StID: keep if _n==1
append using "$P_Data_Processed/oil_pcresponse_big.dta"
keep StID avgdate
sort StID avgdate
by StID: keep if _n==1
save  "$P_Data_Processed/new_adopter_measures_big.dta", replace


** robustness with "selected" treated units

clear
use "$P_Data_Processed/daily_station_data_e5_7_21.dta"
drop avgdate break_date treat_group treat_period month_treat_period
merge m:1 StID using "$P_Data_Processed/new_adopter_measures_big.dta"

gener break_date = avgdate if _merge==3
gener treat_group = 1 if _merge==3
replace break_date = 10^5 if _merge==1
replace treat_group = 0 if missing(treat_group)
drop _merge

gener treat_period = 1 if weekly_date>=break_date
replace treat_period = 0 if weekly_date<break_date

gegen month_treat_period = max(treat_period), by(StID year_month)

gegen min_date_StID = min(date), by(StID year_month)
keep if date==min_date_StID
drop min_date_StID

by year_month Post, sort: gener postal_n_stations = _N
gener postal_n_others = postal_n_stations-1
drop if Post==0

** defining treatment 
capture drop treat
gener treat = month_treat_period*treat_group
label variable treat "Adopter" 

**
** drop outlier observations (top and bottom 1%)
drop if mean_wh_margin<0
drop if mean_wh_margin>0.2
drop if mean_price<1.18
drop if mean_price>1.7
	

sort StID year_month
by StID: gener first_treat_month = year_month if treat==1 & treat[_n-1]==0	
sort StID first_treat_month
by StID: replace first_treat_month = first_treat_month[1]
replace first_treat_month = 1000 if missing(first_treat_month)
	
* number of other adopters in your Postal code
by Post year_month, sort: egen n_compet_adopt = total(treat)
replace n_compet_adopt = n_compet_adopt - 1 if treat==1	
	
** generating simple IV - share of adopters in your brand
by Brand year_month, sort: gener n_brand = _N
by Brand year_month: egen n_treat = total(treat)

gener share_others_treated = (n_treat)/(n_brand-1) if treat==0
replace share_others_treated = (n_treat-1)/(n_brand-1) if treat==1

label variable n_brand "N Brand Stations"
label variable share_others_treated "Share Brand Adopters"
label variable postal_n_stations "N Competitors in ZIP"
label variable n_compet_adopt "N Competitors Adopting"

	
		
merge m:1 StID using "$P_Data_Processed/cluster_mkts.dta"
keep if _merge==3
drop _merge

gener station = 1 
gegen n_act_stations = total(station), by(mktid year_month)


*by year_month mktid, sort: gener mkt_n_stations = _N
gegen n_mkt_compet_adopt = total(treat), by(year_month mktid)
replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if treat==1

capture drop n_months_since_treat 
sort StID year_month
capture drop age 
by StID: gener age = _n
gener n_months_since_treat = year_month - first_treat_month
replace n_months_since_treat=. if n_months_since_treat<-31




ivreghdfe mean_wh_margin  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations==1 , absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_a", append tex(frag) label dec(3) keep(treat)  
	
ivreghdfe mean_wh_margin  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations>1 , absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G4_b", append tex(frag) label dec(3) keep(treat) 
